﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SuperQuery.UserControls.AddDatabaseWizard
{
    /// <summary>
    /// Interaction logic for SelectServerPluginPage.xaml
    /// </summary>
    public partial class SelectServerPluginPage : UserControl, WizardControl.Views.IPage, Views.ISelectServerPluginView
    {
        /// <summary>
        /// Constructor for the Select Server Plugin wizard page
        /// </summary>
        public SelectServerPluginPage()
        {
            InitializeComponent();

            // Hook up the DataContext so that databinding will work
            DataContext = this;

            m_Presenter = new Presenters.SelectServerPluginPresenter(this);
        }

        #region Implemetation of ISelectServerPluginView

        /// <summary>
        /// A dictionary of server plugins available for use
        /// </summary>
        public Dictionary<Guid,string> AvailableServerPlugins
        {
            get
            {
                return (Dictionary<Guid,string>)GetValue(AvailableServerPluginsProperty);
            }
            set
            {
                SetValue(AvailableServerPluginsProperty, value);
            }
        }
        
        /// <summary>
        /// The ID of the selected server plug in
        /// </summary>
        public Guid SelectedServerPluginID
        {
            get
            {
                Guid result = Guid.Empty;

                if ((lstServerPlugins.SelectedItem != null) && (lstServerPlugins.SelectedItem is KeyValuePair<Guid, string>))
                {
                    // An item in the list is selected so return the plugin id (which is the key)
                    KeyValuePair<Guid, string> selectedPlugin = (KeyValuePair<Guid, string>)lstServerPlugins.SelectedItem;
                    result = selectedPlugin.Key;
                }

                return result;
            }
            set
            {
                // Find the server plug in that goes with the provided id
                if (AvailableServerPlugins.Keys.Contains(value))
                {
                    KeyValuePair<Guid, string> plugIn = AvailableServerPlugins.Where(x => x.Key == value).FirstOrDefault();

                    // If we found an item then select it in the list
                    lstServerPlugins.SelectedItem = plugIn;
                }
            }
        }

        /// <summary>
        /// Display an error message to the user
        /// </summary>
        /// <param name="message">The error message to display</param>
        /// <param name="title">The title to use for the error message</param>
        public void DisplayError(string message, string title)
        {
            MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for AvailableServerPlugins.  This enables animation, styling, binding, etc...
        /// </summary>
        private static readonly DependencyProperty AvailableServerPluginsProperty =
            DependencyProperty.Register("AvailableServerPlugins", typeof(Dictionary<Guid, string>), typeof(SelectServerPluginPage), new UIPropertyMetadata(null));


        #endregion

        #region Implemetation of IPage

        /// <summary>
        /// An object containing data that this page can use
        /// </summary>
        public object InputData
        {
            get;
            set;
        }

        /// <summary>
        /// An object containing data that will be passed to the InputData property of the next page
        /// </summary>
        public object OutputData
        {
            get
            {
                // Return the selected plugin's id
                return SelectedServerPluginID;
            }
            set
            {
                // This is a read only property for this page so do nothing
                // This "set" section has to be here becuase of the IPage interface
            }
        }

        /// <summary>
        /// The wizard control that this page belongs to
        /// </summary>
        public WizardControl.Wizard ParentWizard
        {
            get;
            set;
        }

        /// <summary>
        /// The number of this page
        /// </summary>
        /// <returns>A page number</returns>
        public int PageNumber()
        {
            return 1;
        }

        /// <summary>
        /// The caption to use for this page
        /// </summary>
        /// <returns>A page caption</returns>
        public string PageCaption()
        {
            return "Available Server Plugins";
        }

        /// <summary>
        /// The state of the Finish button
        /// </summary>
        /// <returns>If true then Finishg will be active</returns>
        public bool AllowFinish()
        {
            return false;
        }

        /// <summary>
        /// The state of the Cancel button
        /// </summary>
        /// <returns>If true then the Cancel button will be active</returns>
        public bool AllowCancel()
        {
            return true;
        }

        /// <summary>
        /// The state of the Back button
        /// </summary>
        /// <returns>If true then the Back button will be active</returns>
        public bool AllowBack()
        {
            return false;
        }

        /// <summary>
        /// The state of the Forward button
        /// </summary>
        /// <returns>If true then the Forward button will be active</returns>
        public bool AllowForward()
        {
            return true;
        }

        /// <summary>
        /// Validate data on the current page
        /// </summary>
        /// <returns>If true then the next page will be displayed, otherwise we remain on the current page</returns>
        public bool ValidatePage()
        {
            return m_Presenter.Validate();
        }

        /// <summary>
        /// The next page to progress to when forward is clicked
        /// </summary>
        /// <returns>The next page number</returns>
        public int NextPageNumber()
        {
            return 2;
        }

        /// <summary>
        /// Called just before the page is displayed
        /// </summary>
        public void PageActivated()
        {
            m_Presenter.LoadPlugins();
        }

        /// <summary>
        /// Called just after the page is validated, before the next page is displayed
        /// </summary>
        public void PageDeactivated()
        {
            // Nothing to do here for this page
            return;
        }

        #endregion

        /// <summary>
        /// If a plugin is double clicked then select it and move to the next wizard page
        /// </summary>
        /// <param name="sender">Who raised the event</param>
        /// <param name="e">Details about the event</param>
        private void lstServerPlugins_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (lstServerPlugins.SelectedItem != null)
            {
                ParentWizard.GotoNextPage();
            }
        }

        /// <summary>
        /// Presenter for this window
        /// </summary>
        private readonly Presenters.SelectServerPluginPresenter m_Presenter;
    }
}
